import BetterUtilTools from 'better-util-tools';

// UtilTools
export const UtilTools = new BetterUtilTools();


// 获取页面栈
export function getCurPage() {
	let pages = getCurrentPages();
	let page = pages[pages.length - 1];
	return page;
}

export function showLoading(opts) {
	return uni.showLoading(UtilTools.extend({
		title: '拼命加载中...',
		mask: true
	}, opts));
}
export function hideLoading() {
	uni.hideLoading();
}
export function showToast(opts) {
	hideLoading();
	uni.showToast(UtilTools.extend({
		icon: 'none',
		duration: 2000,
		title: ''
	}, opts));
}
export function showModal(opts = {}) {
	opts.content = opts.title || opts.content
	uni.showModal(UtilTools.extend({
		title: '提示',
		showCancel: false,
		confirmText: '知道了'
	}, opts));
}
// 获取assets下资源
export function getSource(sourcePath) {
	// #ifdef H5
	return new URL(`../../assets/${sourcePath}`, import.meta.url).href;
	// #endif
	// #ifndef H5
	const path = `/src/assets/${sourcePath}`;
	const modules = import.meta.glob('src/assets/**/*', { eager: true });
	return modules[path].default;
	// #endif
}
export function getResource(iconName) {
	return getSource(`images/zMenu/${iconName}`);
}

// 时间格式化
export function getTimeStamp(time) {
	let timeStamp = '';
	if (time.length === 10) {
		timeStamp = new Date(time.replace(/-/g, '/') + ' 00:00:00').getTime();
	} else {
		timeStamp = new Date(time.replace(/-/g, '/')).getTime();
	}
	return timeStamp;
}